﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace Passwords.Biz
{
    public class Category : Entity
    {
        private EntityCollection<PasswordLabel> labels;

        public Category(int id, string name)
            : base(id, name)
        { }

        public Category()
            : base()
        {
        }

        public IList<Password> Passwords
        {
            get
            {
                return new EntityCollection<Password>(this, "Passwords", Context.GetPasswords(this));
            }
        }

        public IList<PasswordLabel> Labels
        {
            get
            {
                if (labels == null) labels = new EntityCollection<PasswordLabel>(this, "Labels", Context.GetLabels(this));
                return labels;
            }

            set
            {
                labels = new EntityCollection<PasswordLabel>(this, "Labels", value);
            }
        }

        public bool ContainsLabel(PasswordLabel label)
        {
            return Labels.Any(l => l.Id == label.Id);
        }

        public void Save()
        {
            Context.Save(this);
            this.EntityState = EntityState.Unchanged;
        }

        public void Delete()
        {
            Context.Delete(this);
            this.EntityState = EntityState.Deleted;
        }
    }
}
